package com.common.library.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Window;

import com.common.library.App;


public class UIUtil {

    /**
     * 获取全局applicationContext
     *
     * @return
     */
    private static Context getContext() {
        return App.getInstance().getApplicationContext();
    }

    public static Resources getResources() {
        return getContext().getResources();
    }

    public static String[] getStringArray(int id) {
        return getResources().getStringArray(id);
    }

    public static Configuration getConfiguration() {
        return getResources().getConfiguration();
    }

    public static Drawable getDrawable(int id) {
        return getResources().getDrawable(id);
    }

    public static int getColor(int id) {
        return getResources().getColor(id);
    }

    public static String getString(int id) {
        return getResources().getString(id);
    }

    public static DisplayMetrics getDisplayMetrics() {
        return getResources().getDisplayMetrics();
    }

    public static int getDisplayWidth() {
        return getDisplayMetrics().widthPixels;
    }

    public static int getDisplayHeight() {
        return getDisplayMetrics().heightPixels;
    }

    public static float getDensity() {
        return getDisplayMetrics().density;
    }

    public static float getDensityDpi() {
        return getDisplayMetrics().densityDpi;
    }

    /**
     * dp转换成px
     */
    public static int dp2px(float dp) {
        return (int) (dp * UIUtil.getDensity() + 0.5f);
    }

    /**
     * px转换成dp
     */
    public static int px2dp(float px) {
        return (int) (px / UIUtil.getDensity() + 0.5f);
    }

    /**
     * 获取状态栏高度
     *
     * @return
     */
    public static int getStatusBarHeight() {
        int statusBarHeight = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    /**
     * 获取导航栏的高度
     *
     * @param
     * @return
     */
    public static int getNavigationBarHeight() {
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }

    /**
     * 获取TitleBar高度
     *
     * @param context
     * @return
     */
    public static int getTitleBarHeight(Activity context) {
        int top = context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
        return top - getStatusBarHeight();
    }

    /**
     * 获取ActionBar的高度
     *
     * @param context
     * @return
     */
    public static int getActionBarHeight(Activity context) {
        if (context.getActionBar() == null) {
            return 0;
        }
        return context.getActionBar().getHeight();
    }


    /**
     * 获取View的绘制区域
     *
     * @param context
     * @return
     */
    public static int getRootView(Activity context) {
        return context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getHeight();
    }

    /**
     * 获取应用区域的高度
     *
     * @param context
     * @return
     */
    public static int getContentViewHeight(Activity context) {
        Rect outRect = new Rect();
        context.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        return outRect.height();
    }
}
